In [27]:
test = '49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d'
In [18]:
value_map = {'0': 0,
'1': 1,
'2': 2,
'3': 3,
'4': 4,
'5': 5,
'6': 6,
'7': 7,
'8': 8,
'9': 9,
'A': 10,
'B': 11,
'C': 12,
'D': 13,
'E': 14,
'F': 15,
'G': 16,
'H': 17,
'I': 18,
'J': 19,
'K': 20,
'L': 21,
'M': 22,
'N': 23,
'O': 24,
'P': 25,
'Q': 26,
'R': 27,
'S': 28,
'T': 29,
'U': 30,
'V': 31,
'W': 32,
'X': 33,
'Y': 34,
'Z': 35,
'a': 36,
'b': 37,
'c': 38,
'd': 39,
'e': 40,
'f': 41,
'g': 42,
'h': 43,
'i': 44,
'j': 45,
'k': 46,
'l': 47,
'm': 48,
'n': 49,
'o': 50,
'p': 51,
'q': 52,
'r': 53,
's': 54,
't': 55,
'u': 56,
'v': 57,
'w': 58,
'x': 59,
'y': 60,
'z': 61,
'!': 62,
'"': 63,
'#': 64,
'$': 65,
'%': 66,
'&': 67,
"'": 68,
'(': 69,
')': 70,
'*': 71,
'+': 72,
',': 73,
'-': 74,
'.': 75,
'/': 76,
':': 77,
';': 78,
'<': 79,
'=': 80,
'>': 81,
'?': 82,
'@': 83,
'[': 84,
'\\': 85,
']': 86,
'^': 87,
'_': 88,
'`': 89,
'{': 90,
'|': 91,
'}': 92,
'~': 93}
In [49]:
def strToBase(value, base):
output = ''
temp_val = 0
for index, char in enumerate(value):
temp_val += value_map[char]
if temp_val < base and index < len(value):
continue
while(temp_val > 1):
temp_val = temp_val/base
new = temp_val % base
last_output = output
output = value_map[new] + last_output
return output
In [51]:
strToBase(test, 64)
In [35]:
10/5
Out[35]:
In [33]:
Out[33]:
if the number is greater than the base devide that number by the base. put the remander of that devison at the beginning of the output, then take the rest of the output of the devision as the input for the next loop unitl you get that number down to a size less than the base, then you simply tack that last value on to the end of the output.
so with 15 to base 2:
15/2 = 7 with a remander of 1 so our output starts with 1 7/2 = 3 with a remander of 1 so now we have 11 3/2 = 1 with a remander of 1 so we have 111 the final 1 is less then our base so we tack that on to the beginning... our final output is 1111 == 15
this same logic applys for any other base.
we create a large map up to what ever base we'd like to create.
this map consists of letters and symbols. a = 10, b = 11, c = 12 etc.
we take the input number%base = far right number
number = number/base
unless number was less than base then we put that number out frount.. we have to map any number past 9 to some character.
multiply each character by the base it came from. example... if we are converting from hex.. FF2
15 16^2 + 1516^1 + 2 * 16^0
In [ ]: